<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<script>
    /*选择价表自动补全*/
    autoCompleteDrug("drugNameId");
    function autoCompleteDrug(inputId){
        var storageId = $("#applyTableId .table-selected").find("td").eq(0).find("input").val();
        var parentId=$("#parentId").val();
        if(!parentId){parentId=''}
        var url="${ctx}/doctor/prescriptionItem/autoComplete?drugClass=A&storageId="+storageId+"&parentId="+parentId;
        autoComplete(inputId,url,
                function(data){
                    var rows = [];
                    for(var i=0; i<data.length; i++){
                        rows[rows.length] = {
                            data:data[i],
                            value:data[i].priceId,
                            result:data[i].drugName
                        };
                    }
                    return rows;
                },function(data, i, max){
                    return data.inputCode+"|"+data.drugName+"|  "+data.packageSpec+"|  "+data.packageUnit+"|  "+data.price+"元 |  "+data.firmName+"|库存量："+data.quantity;
                },function(event,data,formatted){

                     $('#drugCode').attr('value',data.drugCode);
                     $('#drugNameId').attr('value',data.drugName);
                     $('#singleDosage').attr('value',data.dosePerUnit);
                     $('#singleUnit').attr('value',data.doseUnits);
                     $('#firmId').attr('value',data.firmid);
                     $('#firmName').attr('value',data.firmName);
                     $('#price').attr('value',data.price);
                     $('#packageSpec').attr('value',data.packageSpec);
                    $('#packUnit').attr('value',data.packageUnit);
                    $('#packageUnit').attr('value',data.packUnit);
//                    $('#num').attr('value',data.quantity);
                     $('#sumPrice').attr('value',data.price);
                     $('#unit').attr('value',data.minUnits);
                    $('#minUnitsTranslate').attr('value',data.doseUnits);
                     $('#spec').attr('value',data.minSpec);
                     $('#inputCode').attr('value',data.inputCode);
                     $('#priceId').attr('value',data.priceId);
            })
    }
    function itemformSaveLoad(divId,applyDivId,formId,saveUrl,loadUrl,loadApplyUrl){
        if($('#'+formId).parsley().validate()){
            /*添加药品表单隐藏，药品明细列表展开并显示相应的列*/
            $("#prescWesternItemDiv").attr("class","panel col-lg-12");
            hideShowTableTd("itemListTable",'3,4,6,7,8,11','1');
            $("#prescWesternItemFormDiv").hide();
            //formSaveMethod(divId,formId,saveUrl,loadUrl);
            saveUrl=timestamp(saveUrl);
            loadUrl=timestamp(loadUrl);
            loadApplyUrl=timestamp(loadApplyUrl);
            if (flag) {
                flag = false;
                formSubmitInput(formId);
                $("#" + formId + " input,#" + formId + " select").removeAttr("disabled");
                $("#" + formId).ajaxSubmit({
                    type: 'post', // 提交方式 get/post
                    url:saveUrl, // 需要提交的 url
                    success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
                        if (data.code == "success") {
                            toastr.success(data.data);
                            loadDiv(divId, loadUrl);
                            loadDiv(applyDivId, loadApplyUrl);
                        } else if(data.code == 'info'){
                            loadDiv(divId, loadUrl);
                        } else {
                            flag = true;
                            toastr.error(data.data);
                        }
                    }
                });
            }
            return false;
        }
        return false;
    }
    function closeDiv(){
        /*添加药品表单隐藏，药品明细列表展开并显示相应的列*/
        $("#prescWesternItemDiv").attr("class","panel col-lg-12");
        hideShowTableTd("itemListTable",'3,4,6,7,8,11','1');
        $("#prescWesternItemFormDiv").hide();
    }


    function onFrequency(){
        calculatedquantity();
        var frequency = $('#frequency option:selected').text();
        var tag='立即';
        var tag2= '必要时';
        $("#num").removeAttr("data-parsley-required", "true");
        $("#abidance").attr("data-parsley-required", "true");
        if(frequency.indexOf(tag)!=-1 || frequency.indexOf(tag2)!=-1){
            $("#num").attr("data-parsley-required", "true");
            $("#abidance").removeAttr("data-parsley-required", "true");
        }
//        if(frequency == '立即' || frequency == '必要时'){
//            alert($('#frequency option:selected').text());
//            $("#num").attr("data-parsley-required", "true");
//        }
    }

    function calculatedquantity(){
//        var frequency = $('#frequency option:selected').val(); //频次
//        var singleDosage = $("#singleDosage").val();//单次用量
//        var packageSpec = $("#packageSpec").val(); // 包装单位
//        var abidance = $("#abidance").val();//天数
//        var drugCode = $("#drugCode").val();
//        var packageUnit = $("#packageUnit").val();
//        var spec = $("#spec").val();
//        var unit = $("#unit").val();
//        var firmId = $("#firmId").val();
        var inputItemForm = $("#inputItemForm").serialize();

        $.get("${ctx}/doctor/prescriptionItem/calculatedquantity",inputItemForm ,function(result) {
            var num = result.dataMap.num;
            $("#num").val(num);
            var num = $.trim($("#num").val());
            var price = $.trim($("#price").val());
            var sum = Number(num) * Number(price);
            $("#sumPrice").val(sum.toFixed(4));
        });
    }
</script>
<div class="panel-body">
    <form:form id="inputItemForm" data-parsley-validate="" modelAttribute="prescriptionItem" onsubmit="return itemformSaveLoad('prescWesternItemDiv','applyDiv','inputItemForm','${ctx}/doctor/prescriptionItem/save','${ctx}/doctor/prescriptionItem/index?applyId=${prescriptionItem.applyId}&inoroutFlag=0','${ctx}/doctor/prescriptionApply/applyIndex?clinicId=${apply.clinicId}&patientId=${apply.patientId}&visitId=${apply.visitId}');" method="post" class="form-horizontal">
        <form:hidden path="applyId" id="applyId"/>
        <form:hidden path="parent.id" id="parentId"/>
        <div class="form-group">
            <label class="col-sm-2 control-label">药品名称</label>
            <div class="col-sm-4">
                <form:hidden path="unit" id="unit"/>
                <form:hidden path="spec" id="spec"/>
                <form:hidden path="repetition" id="repetition" value="1"/>
                <form:hidden path="inputCode" id="inputCode"/>
                <form:hidden path="drugCode" id="drugCode"/>
                <form:hidden path="priceId" id="priceId"/>
                <form:input path="drugName" htmlEscape="false" class="form-control"  maxlength="40" data-parsley-required="true" id="drugNameId" />
            </div>
            <label class="col-sm-2 control-label">频次</label>
            <div class="col-sm-4">
                <form:select path="frequency" id="frequency" class="form-control" onchange="onFrequency()" data-parsley-required="true">
                    <form:options items="${fns:findFreqList()}" itemLabel="freqDesc" itemValue="id" htmlEscape="false"/>
                </form:select>
            </div>


        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">用药天数</label>
            <div class="col-sm-4">
                <form:input path="abidance" id="abidance" onchange="calculatedquantity()" data-parsley-required="true" htmlEscape="false" class="form-control"  maxlength="40"/>
            </div>
            <label class="col-sm-2 control-label">途径</label>
            <div class="col-sm-4">
                <form:select path="channel" id="channel" class="form-control" data-parsley-required="true" onchange="adminDictFun(this,'isSkin','0')">
                    <form:options items="${fns:getSysDictList('ADMINISTRATION_DICT')}" itemLabel="label" itemValue="id" htmlEscape="false"/>
                </form:select>
            </div>

        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">单次用量</label>
            <div class="col-sm-4">
                <form:input path="singleDosage" id="singleDosage" htmlEscape="false" class="form-control" onchange="calculatedquantity()"  maxlength="40" data-parsley-required="true" data-parsley-type="number"/>
            </div>
            <label class="col-sm-2 control-label">用量单位</label>
            <div class="col-sm-4">
                <input  id="minUnitsTranslate" htmlEscape="false" class="form-control" readonly="true" maxlength="40" data-parsley-required="true" />
                <%--<input type="hidden" id="singleUnit" name="singleUnit">--%>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">药品数量</label>
            <div class="col-sm-4">
                <form:input path="num" id="num" htmlEscape="false" class="form-control"  maxlength="40"  data-parsley-type="number" onchange="changeSumPrice('num','price','sumPrice')"/>
            </div>
            <label class="col-sm-2 control-label">包装单位</label>
            <div class="col-sm-4">
                <input type="hidden" name="packageUnit" id="packageUnit">
                <input  id="packUnit" htmlEscape="false" class="form-control" readonly maxlength="40" data-parsley-required="true" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">单价</label>
            <div class="col-sm-4">
                <form:input path="price" id="price" htmlEscape="false" class="form-control"  maxlength="40" data-parsley-required="true" readonly="true"/>
            </div>
            <label class="col-sm-2 control-label">包装规格</label>
            <div class="col-sm-4">
                <form:input path="packageSpec" id="packageSpec" htmlEscape="false" class="form-control"  maxlength="40" data-parsley-required="true" readonly="true"/>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2 control-label">是否皮试</label>
            <div class="col-sm-4">
                <form:select path="isSkin" id="isSkin" class="form-control" data-parsley-required="true">
                    <form:options items="${fns:getDictList('YES_NO')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
                </form:select>
            </div>
            <label class="col-sm-2 control-label">皮试结果</label>
            <div class="col-sm-4">
                <form:input path="skinResult" id="skinResult" htmlEscape="false" class="form-control"  maxlength="40" />
            </div>
        </div>
        <div class="form-group">

            <label class="col-sm-2 control-label">药品厂商</label>
            <div class="col-sm-4">
                <form:hidden path="firmId" id="firmId"/>
                <form:input path="firmName" htmlEscape="false" class="form-control"  maxlength="40" data-parsley-required="true" id="firmName" readonly="true"/>
            </div>
            <label class="col-sm-2 control-label">总价</label>
            <div class="col-sm-4">
                <form:input path="sumPrice" id="sumPrice" htmlEscape="false" class="form-control"  maxlength="40" data-parsley-required="true" readonly="true"/>
            </div>
        </div>
        <button class="btn btn-primary btn-lg btn-parsley"   type="submit" >插入</button>    <%-- <button class="btn btn-primary btn-lg btn-parsley" type="submit">保存并继续</button> --%>    <button class="btn btn-default  btn-lg btn-parsley" type="button" onclick="closeDiv()">取消</button>
    </form:form>

</div>



